package com.huxiu;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.huxiu.net.GsonRequest;
import com.huxiu.net.NetworkConstants;
import com.huxiu.net.NetworkHelper;
import com.huxiu.net.model.ArticleList;
import com.huxiu.net.model.MyFavoriteDataList;
import com.huxiu.net.responses.MyFavoriteListResponse;
import com.huxiu.utils.Constants;
import com.huxiu.utils.Global;
import com.huxiu.utils.NetUtil;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class ShouCangActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.wode_moren_shoucang)
    private LinearLayout wode_moren_shoucang;
    @ViewInject(R.id.favorite_name)
    private TextView favorite_name;
    @ViewInject(R.id.shoucang_num)
    private TextView shoucang_num;
    @ViewInject(R.id.image_right)
    private ImageView image_right;
    @ViewInject(R.id.favorite_line)
    private View favorite_line;
    @ViewInject(R.id.shoucang_list)
    private ListView list;
    @ViewInject(R.id.layout_back)
    private RelativeLayout back;


    @ViewInject(R.id.nothing_ll)
    private LinearLayout nothing_ll;
    @ViewInject(R.id.nothing_text)
    private TextView nothing_text;

    private MyAdapter adapter;
    //    private String uid;
    //    private String cid;
    private MyFavoriteDataList myData;
    private String result_title_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shou_cang);
        ViewUtils.inject(this);
        initData();
        back.setOnClickListener(this);
        wode_moren_shoucang.setOnClickListener(this);
    }

    private void initData() {
        if (!NetUtil.checkNet(this)) {
            Utils.showToast(R.string.generic_check);
            reLoadView.setVisibility(View.VISIBLE);
            return;
        }
        loadList();
    }

    @OnClick({R.id.reLoadView})
    public void mOnClick(View v) {
        switch (v.getId()) {
            case R.id.reLoadView:
                reLoadView.setVisibility(View.GONE);
                initData();
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200) {
            if (resultCode == RESULT_OK) {//联系人activity返回的data
                loadList();
            } else if (resultCode == 2) {
                result_title_name = data.getStringExtra("favorite_title");
                favorite_name.setText(result_title_name);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.wode_moren_shoucang:
                Intent intent = new Intent(this, ShouCangDetailActivity.class);
                intent.putExtra("favorite_data", myData.toString());
                intent.putExtra("favorite_title", result_title_name);
                startActivityForResult(intent, 200);
                break;
        }
    }

    //获取推荐页列表页数据
    private List<ArticleList> lists = new ArrayList<>();

    private void loadList() {
        String url = NetworkConstants.MY_FAVORITE_LIST_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("page", "1");
        params.put("uid", Global.user.uid);
        GsonRequest<MyFavoriteListResponse> request = new GsonRequest<>(url, Request.Method.POST,
                MyFavoriteListResponse.class, true, params,
                new Response.Listener<MyFavoriteListResponse>() {
                    @Override
                    public void onResponse(MyFavoriteListResponse response) {
                        if (response.isSuccess()) {
                            if (response.data.total == 0) {
                                image_right.setVisibility(View.INVISIBLE);
                                favorite_line.setVisibility(View.INVISIBLE);
                                nothing_ll.setVisibility(View.VISIBLE);
                                nothing_text.setText(R.string.no_shoucang_text);
                                if (myData != null) {
                                    myData = null;
                                }
                                if (lists != null && lists.size() != 0) {
                                    lists.clear();
                                }
                                adapter = new MyAdapter();
                                adapter.notifyDataSetChanged();
                                list.setAdapter(adapter);
                            } else {
                                nothing_ll.setVisibility(View.INVISIBLE);
                                image_right.setVisibility(View.VISIBLE);
                                favorite_line.setVisibility(View.VISIBLE);
                                if (lists != null && lists.size() != 0) {
                                    lists.clear();
                                }
                                Collections.addAll(lists, response.data.datalist[0].article_list);
                                myData = response.data.datalist[0];
                                favorite_name.setText(myData.name);
                                shoucang_num.setText(response.data.datalist[0].article_num + "篇文章");
                                adapter = new MyAdapter();
                                list.setAdapter(adapter);
                            }
                        } else {
                            Utils.showToast(response.error.message);
                        }
                    }
                }, mErrorListener);
        NetworkHelper.getInstance().addToRequestQueue(request);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(ShouCangActivity.this, R.layout.item_shoucang_list, null);
            }
            //获取holder对象
            HomeHolder holder = HomeHolder.getHolder(convertView);
            //设置数据
            final ArticleList mItem = lists.get(position);
            holder.title.setText(lists.get(position).title);
            holder.time.setVisibility(View.GONE);
            holder.my_favorite_list_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ShouCangActivity.this, DetailActivity.class);
                    intent.putExtra(Constants.TUIJIAN_ITEM_KEY, mItem.toString());
                    startActivity(intent);
                }
            });
            return convertView;
        }


    }

    //下面的写法是大神的写法，以后可以用这个。
    static class HomeHolder {
        TextView title, time;
        LinearLayout my_favorite_list_item;

        public HomeHolder(View convertView) {
            title = (TextView) convertView.findViewById(R.id.shoucang_item_title);
            time = (TextView) convertView.findViewById(R.id.shoucang_item_time);
            my_favorite_list_item = (LinearLayout) convertView.findViewById(R.id.my_favorite_list_item);
        }

        public static HomeHolder getHolder(View convertView) {
            HomeHolder holder = (HomeHolder) convertView.getTag();
            if (holder == null) {
                holder = new HomeHolder(convertView);
                convertView.setTag(holder);
            }
            return holder;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}
